<?php


namespace App\Utils\Lib\Syj;


use App\Common\Responses;
use EasyWeChat\Factory;

class WeChat
{
    private $AppID;
    private $Secret;
    private const LoginUrl="https://api.weixin.qq.com/sns/jscode2session";

    public function __construct($AppID='',$Secret='')
    {
        $this->AppID = env('WECHAT_APPID',$AppID);
        $this->Secret = env('WECHAT_SECRET',$Secret);
    }

    public function setCode($code)
    {
        $this->code = $code;
    }

    function Login(){
        if(empty($this->code))return Responses::error(['error'=>'未检测到code'],200,'错误');
        $url=self::LoginUrl."?appid=$this->AppID&secret=$this->Secret&js_code=$this->code&grant_type=authorization_code";
        $arr= json_decode(file_get_contents($url),true);
        return $arr;
    }
    function comPlay(){
        $config = [
            'app_id' => 'wx5f9faea12c841ae5',
            'secret' => 'ff674d765b9d5eb0a2acd61a23e5188c',
            'token' => 'token',
            'response_type' => 'array',
        ];
        $response= Factory::officialAccount($config)->server->serve();
        $response->send();
    }

}
